package com.rongshu.view.submitArticle.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.penpal.bean.ArticleColumnsResponse;
import com.rongshu.view.penpal.bean.ArticleLabelsResponse;
import com.rongshu.view.submitArticle.adapter.SubmitAiticleChildLabelAdapter;
import com.rongshu.view.submitArticle.adapter.SubmitAiticleLabelAdapter;
import com.rongshu.view.submitArticle.bean.LabelBean;
import com.rongshu.view.submitArticle.bean.SavaDraftBody;
import com.rongshu.view.submitArticle.presenter.ArticleSettingPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2018/11/8.
 * description：投稿设置
 * 接收参数：
 * int articleFloderId文集id
 * SavaDraftBody body;文章的标题和内容
 */
public class SubmitArticleSettingActivity extends BaseActivity<ArticleSettingPresenter> implements INetView {


    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.columns)
    RecyclerView columns;
    @BindView(R.id.labels)
    RecyclerView labels;
    @BindView(R.id.agree_checkbox)
    CheckBox agreeCheckbox;
    @BindView(R.id.sure_submit)
    Button submit;
    private List<LabelBean> tagsList;
    private List<LabelBean> labelsList;
    private SubmitAiticleLabelAdapter columnAdapter;
    private SubmitAiticleChildLabelAdapter labelAdapter;
    private int columnId;
    private List<LabelBean> selectLabels;
    private SavaDraftBody body;//草稿的body
    private int articleFloderId;//文集id
    private int draftId;//草稿id
    private int contentLength;//文章长度
    @Override
    protected int initLayout() {
        return R.layout.activity_submit_article_setting;
    }

    @Override
    protected void initView() {
        headMiddle.setText("投稿设置");
        //栏目列表
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this, 4);
        columns.setLayoutManager(gridLayoutManager1);
        tagsList = new ArrayList<>();
        columnAdapter = new SubmitAiticleLabelAdapter(this, tagsList);
        columns.setAdapter(columnAdapter);
        columnAdapter.setAdapterListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {//返回选中栏目的position
                if (object instanceof Integer) {
                    int position = (int) object;
                    columnId = tagsList.get(position).getId();
                    labelsList.clear();
                    if (labelAdapter != null) {
                        labelAdapter.resetSelect();
                    }
                    //选择栏目，去请求该栏目下的标签
                    presenter.getLabelList(columnId);
                }
            }
        });

        //标签列表
        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this, 4);
        labels.setLayoutManager(gridLayoutManager2);
        labelsList = new ArrayList<>();
        labelAdapter = new SubmitAiticleChildLabelAdapter(this, labelsList);
        labels.setAdapter(labelAdapter);
        labelAdapter.setAdapterListener(new AdapterListener() {//返回选中标签的list
            @Override
            public void callBack(Object object) {
                selectLabels = (List<LabelBean>) object;
            }
        });

        agreeCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                submit.setBackgroundResource(b ? R.drawable.rectangle_three_green : R.drawable.rectangle_three_white);
            }
        });
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            body = (SavaDraftBody) intent.getSerializableExtra(Constant.DRAFT_DOBY);
            articleFloderId = intent.getIntExtra(Constant.FOLDER_ID, 0);
            draftId = intent.getIntExtra(Constant.DRAFT_ID, 0);
            contentLength = intent.getIntExtra(Constant.ARTICLE_LENGTH, 0);
        }
        setPresenter(new ArticleSettingPresenter(this), this);
        presenter.getColumnList();
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnClick(R.id.head_left)
    public void onViewClicked() {
        finish();
    }

    @OnClick({R.id.sure_submit, R.id.agree_checkbox, R.id.agreement_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sure_submit:
                if (agreeCheckbox.isChecked()) {
                    if (selectLabels == null || selectLabels.size() == 0) {
                        toast("您还未选择标签，请先选择标签");
                        return;
                    }
                    presenter.submitArticle(body, draftId, articleFloderId, columnId
                            , selectLabels, body.getTopImg(), contentLength);
                } else {
                    toast("未同意投稿协议");
                }
                break;
            case R.id.agreement_tv:
                presenter.toWebAct();
                break;
        }
    }

    @Override
    public void success(Object object, int tag) {
        if (tag == ArticleSettingPresenter.REQUEST_COLUMN_MSG) {
            ArticleColumnsResponse response = (ArticleColumnsResponse) object;
            if (response != null && response.getResponse() != null) {
                tagsList.addAll(response.getResponse().getWenzhangfenlei_list());
                if (tagsList.size() >= 1) {
                    tagsList.get(0).setIsSelected(true);
                    columnId = tagsList.get(0).getId();
                }
                columnAdapter.notifyDataSetChanged();
                //默认去请求第一个栏目下的所有标签
                presenter.getLabelList(columnId);
            }
        } else if (tag == ArticleSettingPresenter.REQUEST_LABEL_MSG) {
            ArticleLabelsResponse response = (ArticleLabelsResponse) object;
            if (response != null && response.getResponse() != null) {
                labelsList.clear();
                labelsList.addAll(response.getResponse());
                labelAdapter.notifyDataSetChanged();
            }
        } else {
            toast("投稿成功");
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
